<?
include("../regglobals.php");
include("../config.php");
?>
<html>
<head>
<title>WPS Install</title>
<style type="text/css">
//<!--
body {  font-family: Arial, Helvetica, sans-serif; font-size: 9pt; color: #FFFFFF}
td   {  font-family: Arial, Helvetica, sans-serif; font-size: 10pt;}
form   {  font-family: Arial, Helvetica, sans-serif; font-size: 10pt}
h1   {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16pt; color: #}
h2  {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12pt; }
//-->
</style>
</head>

<body bgcolor="#0e4f77" text="#FFFFFF" link="#808080" vlink="#808080">


<h1 align="center"><b style=background-color=0066cc>Wap Portal Server Install Script</b></h1>
<table align="center" border="0" cellpading="3" cellspacing="0">
<form name="form" action="<?php echo"$PHP_SELF?$QUERY_STRING"; ?>" method="POST">

<?php
if(($db_host  && $db_user && $db_password && $db_name) && ($install_now))
	{
		//check to see if the value provided can conect to the database
		$mysql_link   =  @MYSQL_CONNECT($db_host, $db_user, $db_password);
		//	 OR DIE("Unable to connect to database,");
		if($mysql_link)
		{
			if($db_name == "")
				{
					//db name cant be null string
					?>
					<tr>
					<td colspan="2" align="center"><br><b>Database name cant be null string<br>Step 1</b></td>
					</tr>
					<?
					print_db_info();
				}
			else
				{
					//ok it worked :)...username and password ok
					$configArray[DBhostname]=$db_host;
					$configArray[DBusername]=$db_user;
					$configArray[DBpassword]=$db_password;
					$configArray[DBname]	=$db_name;
					//createFunctionList($configArray);

					$mysql_result = MYSQL_QUERY("CREATE DATABASE $db_name");

					//$mysql_result = 1;



					//see if error returned == that the db is already there.
					$test = mysql_errno();
					if(($mysql_result) || ($test == 1007))
						{
							//ok that worked
							print("<tr><td colspan=2>");
							allert("Created database for use with Wap Portal Server");
							print("</td></tr>");
							//open up file to use in query
							$query = addslashes(fread(fopen("wps.sql", "r"), filesize("wps.sql")));

							//break up sql query - use function from phpMyAdmin

							$pieces  = split_sql($query);
							//from phpMyAdmin

							for ($i=0; $i<count($pieces); $i++)
								{
    								$pieces[$i] = stripslashes(trim($pieces[$i]));
    								if(!empty($pieces[$i]) && $pieces[$i] != "#")
    									{
											$result = mysql_db_query ($db_name, $pieces[$i]) or mysql_error();
    									}
								}

							//end from phpMyAdmin
?>
<tr>
<td colspan="2" align="center"><br><b>Run Wap Portal Server<br>Step 2</b><br></td>
</tr>
<tr>
<tr>
<td align="center"><input type="hidden" name="runSite" value="1"></td>
</tr>
<?
						}
					else
						{
							//hmmm an error has occured

							print("<tr><td colspan=2>");
							allert("an error has occured");
							print("</td></tr>");

						}
		   		}
		}

		else
			{
				print("<tr><td colspan=2>");
				allert("password / username provided are incorrect<br>\ncheck your configuration file");
				print("</td></tr>");
				print_db_info();
			}
	}

elseif($runSite)
{
	print("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=../admin/index.php\">");
	print("<tr><td colspan=2>");
	allert("You are beeing redirected...");
	print("</td></tr>");
	print("</body></html>");
	exit();
}
else
	{
 		//Wont work in secure mode
 		$configArray[currentDir]=currentDir();

		//$dir = popen('chmod 777 $configArray[currentDir]', 'r');
		//pclose($dir);

		print("<tr><td colspan=2>");
		allert("Will install Wap Portal Server database");
		print("</td></tr>");
?>
		<tr>
		<td align="center"><input type="hidden" name="install_now" value="1"></td>
		</tr>
<?
		print_db_info();
		//print "<tr><td>AbsPath: ".getenv('DOCUMENT_ROOT')."</td></tr>";
	}
?>

<tr>
<td align="center" colspan=2><input type="submit" value="submit" name="submit">
</form>
</td>
</tr>
<tr>
<td colspan=2 align=center>
<?php
	createFunctionList($configArray);
?>
</td>
</tr>
</table>

</body>
</html>

<?php

//Generate list of values at bottom of page
function createFunctionList($configArray)
	{
		if (isset($configArray)){
			print("<table align=center border=1 cellpadding=4 cellspacing=2><tr><td colspan=2 align=center bgcolor=#0066cc><b>Current Values</b></td></tr>");
			foreach($configArray as $key => $value)
			{
				echo "<tr><td>$key</td>  <td>$value</td> </tr>";
			}
			print("</table>");
		}
	}

//Return current dir
function currentDir()
{
	$dir = popen('pwd', 'r');
	if ($line = fgets($dir, 1024))
	{
		$current_dir .= $line;
	}
	pclose($dir);
	return $current_dir;
}

//General allert msg
function allert($string)
{
	print("<table align=center border=1 cellspacing=0 cellpadding=0 bordercolor=Silver><tr><td align=center><b>$string</b></td></tr></table>");
}

//Split up long sql string
function split_sql($sql)
{
    $sql = trim($sql);

    $sql = ereg_replace("#[^\n]*\n", "", $sql);

    $buffer = array();

    $ret = array();

    $in_string = false;

    for($i=0; $i<strlen($sql)-1; $i++)
    {
         if($sql[$i] == ";" && !$in_string)
        	{
            	$ret[] = substr($sql, 0, $i);
    	        $sql = substr($sql, $i + 1);
            	$i = 0;
        	}

        if($in_string && ($sql[$i] == $in_string) && $buffer[0] != "\\")
        	{
            	 $in_string = false;
        	}
        elseif(!$in_string && ($sql[$i] == "\"" || $sql[$i] == "'") && (!isset($buffer[0]) || $buffer[0]
 != "\\"))
        {
			   $in_string = $sql[$i];
        }
        if(isset($buffer[1]))
        {
            $buffer[0] = $buffer[1];
        }
        $buffer[1] = $sql[$i];
     }

    if(!empty($sql))
    {
        $ret[] = $sql;
    }
    return($ret);
}


//Print database information
function print_db_info()
{
global $db_host;
global $db_user;
global $db_password;
global $db_name;
?>
<tr>
<td colspan="2" align="center"><br><b>Database installation<br>Step 1</b><br></td>
</tr>
<tr>
<td>Host Name</td>
<td align="center"><? echo $db_host; ?></td>
</tr>

<tr>
<td>Database Username</td>
<td align="center"><? echo $db_user; ?></td>
</tr>

<tr>
<td>Database Password</td>
<td align="center"><? echo $db_password; ?></td>
</tr>

<tr>
<td>Database Name</td>
<td align="center"><? echo $db_name; ?></td>
</tr>

<tr>
<td>----------</td>
<td align="center">----------</td>
</tr>

<tr>
<td>PHP Version</td>
<td align="center"><? echo phpversion(); ?></td>
</tr>
<?}?>